using System;
using System.Data;
using System.Collections;
using CE.Database;
/// <summary>
/// Summary description for CarDetailDB
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class CarAccidentDB
    {
        public CarAccidentDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarAccident caraccident)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCarAccidentInsertUpdateSingleItem";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
                _sPara.Add("@CarAccidentId", caraccident.CarAccidentId);
                _sPara.Add("@CECarId", caraccident.CECarId);
                _sPara.Add("@DateOfAccident", caraccident.DateOfAccident);
                _sPara.Add("@AccidentDetail", caraccident.AccidentDetail);
                _sPara.Add("@ClaimAmount", caraccident.ClaimAmount);
                _sPara.Add("@ClaimType", caraccident.ClaimType);
                _sPara.Add("@RefCEUserId", caraccident.RefCEUserId);
                _sPara.Add("@Status", caraccident.Status);
                _sPara.Add("@Info1", caraccident.Info1);
                _sPara.Add("@Info2", caraccident.Info2);
                _sPara.Add("@Info3", caraccident.Info3);
                _sPara.Add("@Info4", caraccident.Info4);
                _sPara.Add("@Info5", caraccident.Info5);
                _sPara.Add("@Info6", caraccident.Info6);
                _sPara.Add("@Info7", caraccident.Info7);
                _sPara.Add("@Info8", caraccident.Info8);
                _sPara.Add("@Info9", caraccident.Info9);
                _sPara.Add("@Info10", caraccident.Info10);
                
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        public CarAccident GetCarAccident(Int64 CarId, Int64 CEUserId)
        {
            CarAccident _caraccident = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarAccidentSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarAccidentID", CarId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _caraccident = new CarAccident();
                    _caraccident = FillDataRecord(_dt, 0);
                }
            }
            return _caraccident;
        }

        public CarAccidentList GetCarAccidentList(Int64 CarId, Int64 CEUserId)
        {
            CarAccidentList _caraccidentlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarAccidentSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarID", CarId);
            _spara.Add("@RefCEUserId", CEUserId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName,_spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    int _rowcount = _dt.Rows.Count;
                    for (int i = 0; i < _rowcount; i++)
                    {
                        CarAccident _caraccident = new CarAccident();
                        _caraccident = FillDataRecord(_dt, i);
                        _caraccidentlist.Add(_caraccident);
                    }
                }
            }
            return _caraccidentlist;
        }

        #region Comment
        //public CarDetail GetCar(string GUID)
        //{
        //    CarDetail _cardetail = null;
        //    MyDatabase _db = new MyDatabase();
        //    string _spName = "sprocCarDetailByGUIDSelectSingleItem";

        //    SortedList _spara = new SortedList();
        //    _spara.Add("@GUID", GUID);

        //    using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
        //    {
        //        if (_dt != null && _dt.Rows.Count > 0)
        //        {
        //            _cardetail = new CarDetail();
        //            _cardetail = FillDataRecord(_dt);
        //        }
        //    }
        //    return _cardetail;
        //}

        //public string GetGUID(Int64 CarId)
        //{
        //    MyDatabase _db = new MyDatabase();
        //    string strSQL = "Select ROWGUID from tblCarDetail Where [CECarId] = @CECarID";

        //    SortedList _spara = new SortedList();
        //    _spara.Add("@CECarID", CarId);

        //    object _result = _db.ExecuteScalarSqlStatement(strSQL, _spara);

        //    return _result.ToString();
        //}
        #endregion

        private CarAccident FillDataRecord(DataTable myDataTable, int rowId)
        {
            CarAccident myCarAccident = new CarAccident();
            myCarAccident.CarAccidentId = Convert.ToInt64(myDataTable.Rows[rowId]["CarAccidentId"].ToString());
            myCarAccident.CECarId = Convert.ToInt32(myDataTable.Rows[rowId]["CECarId"].ToString());
            myCarAccident.DateOfAccident = Convert.ToDateTime(myDataTable.Rows[rowId]["DateOfAccident"].ToString());
            myCarAccident.ClaimAmount = Convert.ToSingle(myDataTable.Rows[rowId]["ClaimAmount"].ToString());
            myCarAccident.ClaimType = myDataTable.Rows[rowId]["ClaimType"].ToString();
            myCarAccident.RefCEUserId = Convert.ToInt64(myDataTable.Rows[rowId]["RefCEUserId"].ToString());
            myCarAccident.Status = myDataTable.Rows[rowId]["Status"].ToString();
            return myCarAccident;
        }
    }
}